What is graphql-jit?
The graphql-jit package is designed to optimize the execution of GraphQL queries by compiling them into JavaScript functions. This can significantly improve the performance of GraphQL servers by reducing the overhead associated with parsing and interpreting queries at runtime.
What are graphql-jit's main functionalities?
Query Compilation
This feature allows you to compile a GraphQL query into a JavaScript function, which can then be executed. This reduces the overhead of parsing and interpreting the query at runtime, leading to improved performance.
const { compileQuery } = require('graphql-jit');
const { parse, validate, execute, specifiedRules } = require('graphql');
const schema = /* your GraphQL schema */;
const document = parse(/* your GraphQL query */);
const validationErrors = validate(schema, document, specifiedRules);
if (validationErrors.length > 0) {
throw new Error('Validation failed');
}
const compiledQuery = compileQuery(schema, document, 'QueryName');
const result = compiledQuery.query({}, {}, {});
console.log(result);
Custom Execution Context
This feature allows you to pass a custom execution context to the compiled query function. This can be useful for injecting dependencies or other contextual information needed during query execution.
const { compileQuery } = require('graphql-jit');
const { parse, validate, specifiedRules } = require('graphql');
const schema = /* your GraphQL schema */;
const document = parse(/* your GraphQL query */);
const validationErrors = validate(schema, document, specifiedRules);
if (validationErrors.length > 0) {
throw new Error('Validation failed');
}
const compiledQuery = compileQuery(schema, document, 'QueryName');
const customContext = { /* your custom context */ };
const result = compiledQuery.query({}, {}, customContext);
console.log(result);
Other packages similar to graphql-jit
graphql
The 'graphql' package is the reference implementation of GraphQL for JavaScript. It provides the core functionality for defining schemas, parsing queries, and executing them. While it is highly flexible and widely used, it does not offer the same level of performance optimization as graphql-jit, which focuses on compiling queries to improve execution speed.
apollo-server
Apollo Server is a popular GraphQL server implementation that integrates well with various data sources and provides features like caching, subscriptions, and more. While it offers a rich set of features for building GraphQL APIs, it does not focus on query compilation for performance optimization like graphql-jit.
graphql-tools
graphql-tools is a set of utilities for building and manipulating GraphQL schemas in JavaScript. It provides tools for schema stitching, mocking, and more. While it is useful for schema development and testing, it does not offer the query compilation and execution performance optimizations provided by graphql-jit.
GraphQL JIT
Why?
GraphQL-JS is a very well written runtime implementation of the latest GraphQL spec. However, by compiling to JS, V8 is able to create optimized
code which yields much better performance. graphql-jit
leverages this behaviour of V8 optimization by compiling the queries into functions to significantly improve performance (See benchmarks below)
Benchmarks
$ yarn benchmark skip-json
Starting introspection
graphql-js x 1,155 ops/sec ±1.55% (215 runs sampled)
graphql-jit x 5,961 ops/sec ±5.34% (216 runs sampled)
Starting fewResolvers
graphql-js x 14,313 ops/sec ±1.43% (224 runs sampled)
graphql-jit x 409,587 ops/sec ±1.08% (216 runs sampled)
Starting manyResolvers
graphql-js x 13,201 ops/sec ±1.50% (216 runs sampled)
graphql-jit x 229,025 ops/sec ±1.18% (216 runs sampled)
Starting nestedArrays
graphql-js x 108 ops/sec ±1.30% (216 runs sampled)
graphql-jit x 1,317 ops/sec ±2.38% (213 runs sampled)
Done in 141.94s.
Support for GraphQL spec
The goal is to support the June 2018 version of the GraphQL spec. At this moment,
the only missing feature is support for Subscriptions.
Differences to graphql-js
In order to achieve better performance, the graphql-jit
compiler introduces some limitations.
The primary limitation is that all computed properties must have a resolver and only these can return a Promise
.
Install
yarn add graphql-jit
Example
For complete working examples, check the examples/ directory
Create a schema
const typedefs = `
type Query {
hello: string
}
`;
const resolvers = {
Query: {
hello() {
return new Promise(resolve => setTimeout(() => resolve("World!"), 200));
}
}
};
const { makeExecutableSchema } = require("graphql");
const schema = makeExecutableSchema({ typedefs, resolvers });
Compile a Query
const query = `
{
hello
}
`;
const { parse } = require("graphql");
const document = parse(query);
const { compileQuery, isCompiledQuery } = require("graphql-jit");
const compiledQuery = compileQuery(schema, document);
if (!isCompiledQuery(compiledQuery)) {
console.error(compiledQuery);
throw new Error("Error compiling query");
}
Execute the Query
const executionResult = await compiledQuery.query();
console.log(executionResult);
API
compiledQuery = compileQuery(schema, document, operationName, compilerOptions)
Compiles the document
AST, using an optional operationName and compiler options.
-
schema
{GraphQLSchema} - graphql
schema object
-
document
{DocumentNode} - document query AST ,can be obtained by parse
from graphql
-
operationName
{string} - optional operation name in case the document contains multiple operations(queries/mutations/subscription).
-
compilerOptions
{Object} - Configurable options for the compiler
disableLeafSerialization
{boolean, default: false} - disables leaf node serializers. The serializers validate the content of the field at runtime
so this option should only be set to true if there are strong assurances that the values are valid.customSerializers
{Object as Map, default: {}} - Replace serializer functions for specific types. Can be used as a safer alternative
for overly expensive serializerscustomJSONSerializer
{boolean, default: false} - Whether to produce also a JSON serializer function using fast-json-stringify
. The default stringifier function is JSON.stringify
compiledQuery.compiled(root: any, context: any, variables: Maybe<{ [key: string]: any }>)
the compiled function that can be called with a root value, a context and the required variables.
compiledQuery.stringify(value: any)
the compiled function for producing a JSON string. It will be JSON.stringify
unless compilerOptions.customJSONSerializer
is true.
The value argument should the return of the compiled GraphQL function.
LICENSE
MIT